/**
 * 
 */
package org.footito.android.api;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Target : http://api.footito.fr/avatar/groups/
 * http://api.footito.fr/avatar/groups/?updated_after=###TON_TIMESTAMP####
 * 
 * @author Julien
 * 
 */
public class GroupsTeamApiHandler extends DefaultHandler {

	public final static String URL_API = "http://api.footito.fr/avatar/groups/";
	public final static String URL_API_UPDATED_AFTER = "updated_after";

	private String name;
	private String picture;
	private String link;
	private int id;
	private ArrayList<GroupsTeamFromAPI> mTeamList = new ArrayList<GroupsTeamFromAPI>();
	private int group;

	/**
	 * 
	 */
	public GroupsTeamApiHandler() {
	}
	
	public ArrayList<GroupsTeamFromAPI> getGroupsTeamList() {
		return mTeamList;
	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (localName.equals(TAG.group)) {
			IN.group = true;
			id = Integer.parseInt(attributes.getValue(TAG.id));
		} else if (localName.equals(TAG.name)) {
			IN.name = true;
			name = "";
		} 
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (localName.equals(TAG.group)) {
			IN.group = false;
			GroupsTeamFromAPI currentTeam = new GroupsTeamFromAPI(id, name);
			if (currentTeam.isValid())
				mTeamList.add(currentTeam);
		} else if (localName.equals(TAG.name)) {
			IN.name = false;
		}
	}

	public void characters(char[] ch, int start, int length) throws SAXException {
		if (IN.name) {
			this.name += new String(ch, start, length);
		} 
	};
	private static class IN {
		private static boolean groups;
		private static boolean group;
		private static boolean name;
	}

	private static class TAG {
		private final static String groups = "groups";
		private final static String group = "group";
		private final static String name = "name";
		private final static String id = "id";
	}
}
